﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tunnel.Entity;

namespace Tunnel.Tools.FcmsTools
{
    public class FcmsPlayList
    {
        private List<DisplayElement> elementList = new List<DisplayElement>();

        public List<DisplayElement> AddItem(DisplayElement element)
        {
            this.elementList.Add(element);
            return this.elementList;
        }

        public void ClearItem()
        {
            this.elementList.Clear();
        }

        public List<byte> GetPlayList()
        {
            List<byte> list = new List<byte>();
            

            List<byte> startString = Encoding.ASCII.GetBytes("[Playlist]\r\nItem_No=").ToList<byte>();
            List<byte> itemString = Encoding.ASCII.GetBytes("Item").ToList<byte>();
            List<byte> enterString = Encoding.ASCII.GetBytes("\r\n").ToList<byte>();

            int count = elementList.Count;

            list.AddRange(startString);
            list.AddRange(Encoding.ASCII.GetBytes(count.ToString()));
            list.AddRange(enterString);

            while(count>0)
            {

                int index = elementList.Count - count;
                list.AddRange(itemString); //item
                list.AddRange(Encoding.ASCII.GetBytes(index.ToString())); //序数
                //list.Add(0x20); //空格
                list.Add(0x3d); //等于号
                //list.Add(0x20); //空格

                list.AddRange(Encoding.ASCII.GetBytes(elementList[index].Delay));
                list.Add(0x2c); //逗号
                //list.Add(0x20); //空格

                switch(elementList[index].ShowType)
                {
                    case "立即出现":list.Add(0x31); break;
                    case "上移": list.Add(0x32); break;
                    case "下移": list.Add(0x33); break;
                    case "左移": list.Add(0x34); break;
                    case "右移": list.Add(0x35); break;
                    default:list.Add(0x30);break;
                }
                list.Add(0x2c); //逗号
                //list.Add(0x20); //空格

                list.AddRange(Encoding.ASCII.GetBytes(elementList[index].Speed));
                list.Add(0x2c); //逗号
                //list.Add(0x20); //空格

                if (!string.IsNullOrEmpty(this.elementList[index].image))
                {
                    //增加添加IMAGE内容
                }
                list.Add(0x5c); //反斜杠
                list.Add(0x43); //大写C
                list.AddRange(GetXY(elementList[index].MsgX, elementList[index].MsgY));
                list.Add(0x5c); //反斜杠
                list.Add(0x66); //小写f

                switch (elementList[index].Font)
                {
                    case "黑体": list.Add(0x68); break;
                    case "楷体": list.Add(0x6b); break;
                    case "宋体": list.Add(0x73); break;
                    case "仿宋": list.Add(0x66); break;                    
                }

                list.AddRange(Encoding.ASCII.GetBytes(elementList[index].FontSize));

                list.Add(0x5c); //反斜杠
                list.Add(0x63); //小写c
                switch (elementList[index].Colour)
                {
                    case "红色": {
                            list.Add(0x32);
                            list.Add(0x35);
                            list.Add(0x35);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                        }; break;
                    case "黄色": {
                            list.Add(0x32);
                            list.Add(0x35);
                            list.Add(0x35);
                            list.Add(0x32);
                            list.Add(0x35);
                            list.Add(0x35);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);

                        }; break;
                    case "蓝色": {
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x32);
                            list.Add(0x35);
                            list.Add(0x35);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                        }; break;
                    case "绿色": {
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x32);
                            list.Add(0x35);
                            list.Add(0x35);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                            list.Add(0x30);
                        }; break;
                }

                list.AddRange(Encoding.Default.GetBytes(elementList[index].Msg));
                list.AddRange(enterString);
                count--;
            }

            return list;
        }

        private List<byte> GetXY(string x,string y)
        {
            string newx;

            switch (x.Length)
            {
                case 0: newx = "000";break;
                case 1: newx = "00" + x;break;
                case 2: newx = "0" + x;break;
                case 3: newx = x;break;
                default: newx = "000";break;
            }

            string newy;

            switch (y.Length)
            {
                case 0: newy = "000"; break;
                case 1: newy = "00" + y; break;
                case 2: newy = "0" + y; break;
                case 3: newy = y; break;
                default: newy = "000"; break;
            }

            List<byte> xy = new List<byte>();
            xy.AddRange(Encoding.ASCII.GetBytes(newx));
            xy.AddRange(Encoding.ASCII.GetBytes(newy));

            return xy;
        }
    }
}
